package cn.sczc.jh.client.service.equipment;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.sczc.jh.client.entity.Equipment;
import cn.sczc.jh.client.mapper.equipment.EquipmentMapper;
import cn.sczc.jh.client.util.DateUtil;

@Service
public class EquipmentService extends ServiceImpl<EquipmentMapper, Equipment>{
	@Autowired
	private EquipmentMapper equipmentMapper;
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<Equipment> queryList(Page<Equipment> page, Map<String, Object> params) {
		List<Equipment> list =  equipmentMapper.queryList(page,params);
		if (list!=null && list.size()>0) {
			for (Equipment equipment : list) {
				int days = DateUtil.getDayBetween(new Date(),equipment.getMaintenanceDate());
				if (days>=0) {
					if (days>15) {
						equipment.setState("<span style='color:#49d58b'>正常</span>");
					}else {
						equipment.setState("<span style='color:#f8da35'>还有"+days+"天到期</span>");
					}
				}else {
					equipment.setState("<span style='color:#e7383b'>已过期</span>");
				}
			}
		}
		page.setRecords(list);
		return page;
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Equipment> queryListNoPage(Map<String, Object> params) {
		List<Equipment> list = equipmentMapper.queryListNoPage(params);
		if (list!=null && list.size()>0) {
			for (Equipment equipment : list) {
				int days = DateUtil.getDayBetween(new Date(),equipment.getMaintenanceDate());
				if (days>=0) {
					if (days>15) {
						equipment.setState("正常");
					}else {
						equipment.setState("还有"+days+"天到期");
					}
				}else {
					equipment.setState("已过期");
				}
			}
		}
		return list;
	}

	public Boolean insert(Equipment entity) {
		int a = 0;
		if (entity.getOid()!=null && !"".equals(entity.getOid())) {//修改
			a = equipmentMapper.updateById(entity);
		}else {//新增
			entity.setOid(UUID.randomUUID().toString());
			entity.setCreateTime(new Date());
			a = equipmentMapper.insert(entity);
		}
		return a==1;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Equipment qryById(String id) {
		return equipmentMapper.selectById(id);
	}

	public Boolean del(String id) {
		int a = 0;
		a = equipmentMapper.deleteById(id);
		return a==1;
	}
	
}
